﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Exercises.Actions
{
    public class BlockByIPAttribute : ActionFilterAttribute
    {
        public string Networks { get; set; }

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            string userIP = filterContext.HttpContext.Request.UserHostAddress;

            foreach (string ip in Networks.Split(';'))
            {
                if (userIP.Equals(ip.Trim()))
                    filterContext.Result = new ViewResult() { ViewName = "BannedIP" };
            }

        }
    }
}
